package com.ktg.mes.md.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.core.domain.TreeSelect;
import com.ktg.common.utils.DateUtils;
import com.ktg.common.utils.StringUtils;
import com.ktg.mes.md.domain.ItemType;
import com.ktg.mes.md.domain.MdStatistics;
import com.ktg.mes.md.domain.MdStatisticsTypeBom;
import com.ktg.mes.md.mapper.MdStatisticsMapper;
import com.ktg.mes.md.mapper.MdStatisticsTypeBomMapper;
import com.ktg.mes.md.service.IMdStatisticsService;
import com.ktg.mes.md.service.IMdStatisticsTypeBomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 单位Service业务层处理
 *
 * @author ruoyi
 * @date 2022-04-27
 */
@Service
public class MdStatisticsServiceImpl  extends ServiceImpl<MdStatisticsMapper, MdStatistics> implements IMdStatisticsService
{

    /**
     * 查询单位
     *
     * @param measureId 单位主键
     * @return 单位
     */
    @Override
    public MdStatistics selectMdStatisticsByMeasureId(Long measureId)
    {
        return getBaseMapper().selectMdStatisticsByMeasureId(measureId);
    }

    /**
     * 查询单位列表
     *
     * @param mdStatistics 单位
     * @return 单位
     */
    @Override
    public List<MdStatistics> selectMdStatisticsList(MdStatistics mdStatistics)
    {
        return getBaseMapper().selectMdStatisticsList(mdStatistics);
    }

    /**
     * 新增单位
     *
     * @param mdStatistics 单位
     * @return 结果
     */
    @Override
    public int insertMdStatistics(MdStatistics mdStatistics)
    {
        mdStatistics.setCreateTime(DateUtils.getNowDate());
        boolean a = save(mdStatistics);
        if (a){
            return 1;
        }
        return 0;
    }

    /**
     * 修改单位
     *
     * @param mdStatistics 单位
     * @return 结果
     */
    @Override
    public int updateMdStatistics(MdStatistics mdStatistics)
    {
        mdStatistics.setUpdateTime(DateUtils.getNowDate());
        boolean a = updateById(mdStatistics);
        if (a){
            return 1;
        }
        return 0;
    }

    /**
     * 批量删除单位
     *
     * @param measureIds 需要删除的单位主键
     * @return 结果
     */
    @Override
    public int deleteMdStatisticsByMeasureIds(Long[] measureIds)
    {
        return getBaseMapper().deleteMdStatisticsByMeasureIds(measureIds);
    }

    /**
     * 删除单位信息
     *
     * @param measureId 单位主键
     * @return 结果
     */
    @Override
    public int deleteMdStatisticsByMeasureId(Long measureId)
    {
        return getBaseMapper().deleteMdStatisticsByMeasureId(measureId);
    }


    @Override
    public List<TreeSelect> buildTreeSelect(List<MdStatistics> list) {
        List<MdStatistics> itemTypes = buildTree(list);
        return itemTypes.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    private List<MdStatistics> buildTree(List<MdStatistics> itemTypes){
        List<MdStatistics> returnList = new ArrayList<MdStatistics>();
        List<Long> tempList = new ArrayList<Long>();
        for(MdStatistics it : itemTypes){
            tempList.add(it.getStatisticsId());
        }

        for(MdStatistics it : itemTypes){
            if(!tempList.contains(it.getParentTypeId())){
                recursionFn(itemTypes,it);
                returnList.add(it);
            }
        }
        if(returnList.isEmpty()){
            returnList = itemTypes;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<MdStatistics> list, MdStatistics t)
    {
        // 得到子节点列表
        List<MdStatistics> childList = getChildList(list, t);
        t.setChildren(childList);
        for (MdStatistics tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<MdStatistics> getChildList(List<MdStatistics> list, MdStatistics t)
    {
        List<MdStatistics> tlist = new ArrayList<MdStatistics>();
        Iterator<MdStatistics> it = list.iterator();
        while (it.hasNext())
        {
            MdStatistics n = (MdStatistics) it.next();
            if (StringUtils.isNotNull(n.getParentTypeId()) && n.getParentTypeId().longValue() == t.getStatisticsId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<MdStatistics> list, MdStatistics t)
    {
        return getChildList(list, t).size() > 0;
    }


}
